package com.buka.recipe.common.utils;

import lombok.extern.log4j.Log4j2;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;
import java.util.UUID;

/**
 * @author admin
 */
@Log4j2
public class ImageUtils {

    public static final String IMAGE_SUFFIX_TYPE_JPEG = "data:image/jpeg;base64,";
    public static final String IMAGE_SUFFIX_TYPE_PNG = "data:image/png;base64,";
    public static final String IMAGE_SUFFIX_TYPE_BMP = "data:image/bmp;base64,";
    public static final String IMAGE_SUFFIX_TYPE_JPG = "data:image/jpg;base64,";

    /**
     * Base64转本地图片文件
     *
     * @param base64Str 图片的Base64字符串
     * @param savePath  待保存的本地路径
     * @return 是否成功
     */
    public static String generateImage(String base64Str, String savePath) {
        if (!StringUtils.hasLength(base64Str)) {
            return null;
        }
        String suffix;
        if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPEG)) {
            suffix = ".jpeg";
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_JPEG.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_PNG)) {
            suffix = ".png";
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_PNG.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_BMP)) {
            suffix = ".bmp";
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_BMP.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPG)) {
            suffix = ".jpg";
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_JPG.length());
        } else {
            return null;
        }
        File savePathFile = new File(savePath);
        if (!savePathFile.exists()) {
            boolean res = savePathFile.mkdirs();
            log.info("mkdir imageSavePath result {}", res);
        }
        String fileName = UUID.randomUUID().toString().concat(suffix);
        Base64.Decoder decoder = Base64.getDecoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decode(base64Str);
            for (int i = 0; i < bytes.length; ++i) {
                // 调整异常数据
                if (bytes[i] < 0) {
                    bytes[i] += (byte) 256;
                }
            }
            File file = new File(savePath, fileName);
            log.debug("image file path {}", file.getAbsolutePath());
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(file);
            out.write(bytes);
            out.flush();
            out.close();
            return fileName;
        } catch (IOException e) {
            log.error("save image error, {}", e.getMessage());
        }
        return null;
    }

    /**
     * Base64转 byte[]
     *
     * @param base64Str 图片的Base64字符串
     * @return 是否成功
     */
    public static byte[] convertBase64ToByte(String base64Str) {
        if (!StringUtils.hasLength(base64Str)) {
            return null;
        }
        if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPEG)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_JPEG.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_PNG)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_PNG.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_BMP)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_BMP.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPG)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_JPG.length());
        } else {
            return null;
        }
        Base64.Decoder decoder = Base64.getDecoder();
        // Base64解码
        byte[] bytes = decoder.decode(base64Str);
        for (int i = 0; i < bytes.length; ++i) {
            // 调整异常数据
            if (bytes[i] < 0) {
                bytes[i] += (byte) 256;
            }
        }
        return bytes;
    }

    /**
     * 得到base64图片的类型
     *
     * @param base64Str 图片的Base64字符串
     * @return 图片的类型
     */
    public static String getImageType(String base64Str) {
        if (!StringUtils.hasLength(base64Str)) {
            return null;
        }
        String suffix = null;
        if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPEG)) {
            suffix = ".jpeg";
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_PNG)) {
            suffix = ".png";
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_BMP)) {
            suffix = ".bmp";
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPG)) {
            suffix = ".jpg";
        }
        return suffix;
    }

    /**
     * 得到base64图片的类型
     *
     * @param base64Str 图片的Base64字符串
     * @return 图片的内容
     */
    public static String getImageContent(String base64Str) {
        if (!StringUtils.hasLength(base64Str)) {
            return null;
        }
        if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPEG)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_JPEG.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_PNG)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_PNG.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_BMP)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_BMP.length());
        } else if (base64Str.startsWith(IMAGE_SUFFIX_TYPE_JPG)) {
            base64Str = base64Str.substring(IMAGE_SUFFIX_TYPE_JPG.length());
        }
        return base64Str;
    }

}
